//
//  KRMessageNoticeViewController.m
//  KEREN
//
//  Created by MW on 2023/4/20.
//

#import "KRMessageNoticeViewController.h"
#import "KRFaultViewController.h"
#import "KRAlarmViewController.h"
#import "KRMessageViewController.h"
@interface KRMessageNoticeViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) UIView *currentLineView;
@property (nonatomic, strong) UILabel *dateLbl;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIView *deteView;
@property (nonatomic, assign) NSInteger time_stamp;
@property (nonatomic, strong) KRFaultViewController *csVC;
@property (nonatomic, strong) KRAlarmViewController *tlVC;
@property (nonatomic, strong) KRMessageViewController *crVC;
@end

@implementation KRMessageNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavViewWithRightTitle:@"标记全部已读" navColorIswhite:YES withTitle:@"消息通知"];
    WEAKSELF
    self.rightClickBlock = ^(void){
        [weakSelf messageNotifyupdate:weakSelf.index + 1];
    };
    self.rightLbl.text = @"";
    
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(60 + STATUSBAR_HEIGHT);
        make.height.offset(48);
        make.left.right.offset(0);
    }];
    topView.backgroundColor = Whire_Color;
    //DOTO国际化
    NSArray *titleArr = @[@"故障",@"告警",@"通知"];
    for (int i = 0; i < titleArr.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/3 * i, 0, DEVICE_WIDTH/3, 47)];
        [topView addSubview:btn];
        if(i == 0){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            _currentLineView = [[UIView alloc] init];
            [topView addSubview:_currentLineView];
            [_currentLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(18);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
            _currentLineView.backgroundColor = Color_7ACC00;
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArr addObject:btn];
    }
    [UIView setLineViewWithframe:CGRectMake(0, 47, DEVICE_WIDTH, 1) forinView:topView];
    
    _deteView = [[UIView alloc] initWithFrame:CGRectMake(0, 108 + STATUSBAR_HEIGHT, DEVICE_WIDTH, 60)];
    [self.view addSubview:_deteView];
    _deteView.backgroundColor = Whire_Color;
    _deteView.userInteractionEnabled = YES;
    UILabel *datechooseLbl = [UILabel setLblText:@"日期选择" font:Font(16) withColor:Color_333333 inSubView:_deteView];
    [datechooseLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.centerY.equalTo(_deteView);
    }];
    
    UIImageView *rightIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Vector"]];
    [_deteView addSubview:rightIcon];
    [rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-24);
        make.width.offset(8);
        make.height.offset(14);
        make.centerY.equalTo(_deteView);
    }];
    _dateLbl = [UILabel setLblText:[DeteManager shareInstance].getTodayStr font:Font(14) withColor:Color_86898E inSubView:_deteView];
    [_dateLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(rightIcon.mas_right).offset(-12);
        make.centerY.equalTo(_deteView);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    [_deteView addGestureRecognizer:tap];
    _deteView.hidden = YES;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108 + STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT)];
    [self.view addSubview:_scrollView];
    
    _csVC = [[KRFaultViewController alloc] init];
    _tlVC = [[KRAlarmViewController alloc] init];
    _crVC = [[KRMessageViewController alloc] init];
    _tlVC.time_stamp = [[DeteManager shareInstance] getCurrentDay];
    _crVC.time_stamp = [[DeteManager shareInstance] getCurrentDay];
    _time_stamp = [[DeteManager shareInstance] getCurrentDay];
    [self addChildViewController:_csVC];
    [self addChildViewController:_tlVC];
    [self addChildViewController:_crVC];
    _csVC.view.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    _tlVC.view.frame = CGRectMake(DEVICE_WIDTH, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT - 60);
    _crVC.view.frame = CGRectMake(DEVICE_WIDTH * 2, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT - 60);
    
    [_scrollView addSubview:_csVC.view];
    [_scrollView addSubview:_tlVC.view];
    [_scrollView addSubview:_crVC.view];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(DEVICE_WIDTH * 3, 0);
    _scrollView.delegate = self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[KRAlartManager shareInstance] resettingDeteRow];
}

- (void)btnClick:(UIButton *)sender{
    [self updateTopView:sender.tag];
    [self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH * sender.tag, 0) animated:NO];
}

- (void)messageNotifyupdate:(NSInteger)messageNotifyID{
    [[KRAlartManager shareInstance] showLodingAlertView:@"努力加载中..."];
    WEAKSELF
    NSDictionary *parameters = @{@"message_type":@(messageNotifyID),@"flag":@(1),@"time_stamp":@(_time_stamp)};
    [NetworkingTool POST:@"/v1/app/messageNotify/update" parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.crVC getmessageNotifyList];
        [weakSelf.tlVC getmessageNotifyList];
        [[KRAlartManager shareInstance] hidenLodingImageView];
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    float x = scrollView.contentOffset.x + DEVICE_WIDTH /2;
    int index = x / DEVICE_WIDTH ;
    [self updateTopView:index];
}

- (void)updateTopView:(NSInteger)tag{
    if(tag == 0){
        self.rightLbl.text = @"";
        _deteView.hidden = YES;
        _scrollView.frame = CGRectMake(0, 108 + STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    }else{
        _scrollView.frame = CGRectMake(0, 108 + STATUSBAR_HEIGHT + 60, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT - 60);
        self.rightLbl.text = @"标记全部已读";
        _deteView.hidden = NO;
    }
    _index = tag;
    for (UIButton *btn in _btnArr) {
        if(btn.tag == tag){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            [_currentLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(18);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
    }
}

- (void)tapClick{
    WEAKSELF
    [[KRAlartManager shareInstance] showPickAlertView:@"选择日期" withcurrentIndex:1 IscanChooseHistory:YES  changeTitleBlock:^(NSString * _Nonnull title) {
        weakSelf.dateLbl.text = title;
        weakSelf.crVC.time_stamp = [[DeteManager shareInstance] monthDayWithdeteStr:title];
        weakSelf.tlVC.time_stamp = [[DeteManager shareInstance] monthDayWithdeteStr:title];
        weakSelf.time_stamp = [[DeteManager shareInstance] monthDayWithdeteStr:title];
        [weakSelf.crVC getmessageNotifyList];
        [weakSelf.tlVC getmessageNotifyList];
        [[KRAlartManager shareInstance] showLodingAlertView:@"努力加载中..."];
    }];
}

- (NSMutableArray *)btnArr{
    if(!_btnArr){
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}
@end
